package com.zfzs.post.modules.finance.service.impl;

import com.zfzs.post.modules.agent.domain.Agent;
import com.zfzs.post.modules.agent.domain.AgentVerified;
import com.zfzs.post.modules.agent.service.AgentService;
import com.zfzs.post.modules.agent.service.AgentVerifiedService;
import com.zfzs.post.modules.finance.service.FlowActivWebService;
import com.zfzs.post.modules.finance.service.vo.FlowActivWebVO;
import com.zfzs.post.modules.flow.domain.FlowActiv;
import com.zfzs.post.modules.flow.service.FlowActivService;
import com.zfzs.post.modules.flow.service.dto.FlowActivQueryCriteria;
import com.zfzs.post.modules.system.service.UserService;
import com.zfzs.post.utils.CloneUtils;
import com.zfzs.post.utils.SecurityUtils;
import com.zfzs.post.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wq
 * @date 2019/12/6 11:34
 * @description
 */
@Service
public class FlowActivWebServiceImpl implements FlowActivWebService {

    @Autowired
    private FlowActivService flowActivService;

    @Autowired
    private AgentVerifiedService agentVerifiedService;


    @Override
    public Map<String, Object> findPageList(FlowActivQueryCriteria criteria, Integer page, Integer size) {

        Long id = SecurityUtils.getUserId();

        //管理员数据
        if (!id.equals(1L)) {
            criteria.setChannelId(id);
        }

        if (StringUtils.isNotBlank(criteria.getReapName())) {
            List<AgentVerified> reaps = agentVerifiedService.findByName(criteria.getReapName());
            criteria.setAgentIds(reaps.stream().map(AgentVerified::getId).collect(Collectors.toList()));
        }


        if (StringUtils.isNotBlank(criteria.getActivName())) {
            List<AgentVerified> reaps = agentVerifiedService.findByName(criteria.getReapName());
            criteria.setActivAgentIds(reaps.stream().map(AgentVerified::getId).collect(Collectors.toList()));
        }

        Page<FlowActiv> pageInfo = flowActivService.findPage(criteria, page, size);

        List<FlowActiv> content = pageInfo.getContent();

        List<FlowActivWebVO> list = new ArrayList<>();

        for (FlowActiv flowActiv : content) {

            FlowActivWebVO vo = CloneUtils.clone(flowActiv, FlowActivWebVO.class);

            AgentVerified reap = agentVerifiedService.findById(vo.getAgentId());

            vo.setReapName(reap.getCardName());

            if (vo.getActivId().equals(vo.getAgentId())) {
                vo.setActivName(reap.getCardName());
            } else {
                AgentVerified activ = agentVerifiedService.findById(vo.getActivAgent());

                vo.setActivName(activ.getCardName());
            }

            list.add(vo);

        }

        Map<String, Object> map = new HashMap<>();
        map.put("content", list);
        map.put("totalElements", pageInfo.getTotalElements());
        return map;
    }
}
